package cn.cmcc.intertelecom.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.cmcc.intertelecom.bean.Role;
import cn.cmcc.intertelecom.bean.RoleGroup;
import cn.cmcc.intertelecom.bean.RoleGroupExample;
import cn.cmcc.intertelecom.bean.RoleGroupExample.Criteria;
import cn.cmcc.intertelecom.bean.User;
import cn.cmcc.intertelecom.dao.RoleGroupMapper;

@Service
public class RoleGroupService {
	@Autowired
	private RoleGroupMapper roleGroupMapper;

	/**
	 * 根据用户的id获取RoleGroup
	 * @param userId
	 * @return
	 */
	public List<RoleGroup> getRoleByUserId(Integer userId) {
		RoleGroupExample example=new RoleGroupExample();
		Criteria criteria = example.createCriteria();
		criteria.andUserIdEqualTo(userId);
		
		return roleGroupMapper.selectByExample(example);
	}

	/**
	 * 保存 用户id和角色 也就是建立用户和角色之间的关系
	 * @param roleGroup
	 */
	public void saveUserRole(RoleGroup roleGroup) {
		//1.删除该id下的所有角色
		RoleGroupExample example=new RoleGroupExample();
		Criteria criteria = example.createCriteria();
		criteria.andUserIdEqualTo(roleGroup.getUserId());
		
		roleGroupMapper.deleteByExample(example);
		
		
		//2.储存用户id和角色
		roleGroup.setCreatedate(new Date());
		for (int i = 0; i < roleGroup.getRoleIds().length; i++) {
			roleGroup.setRoleId(roleGroup.getRoleIds()[i]);
			roleGroupMapper.insertSelective(roleGroup);
		}

		
	}

	/**
	 * 根据用户的id 批量删除所有的用户
	 * @param ids
	 */
	public void deleteBatchByuserids(List<Integer> userIds) {
		RoleGroupExample example =new RoleGroupExample();
		Criteria criteria = example.createCriteria();
		criteria.andUserIdIn(userIds);
		roleGroupMapper.deleteByExample(example);
		
	}

	/**
	 * 根据用户的id 删除所有的用户
	 * @param parseInt
	 */
	public void deleteByuserid(int userId) {
		RoleGroupExample example =new RoleGroupExample();
		Criteria criteria = example.createCriteria();
		criteria.andUserIdEqualTo(userId);
		roleGroupMapper.deleteByExample(example);
		
	}

	/**
	 * 根据角色id 批量删除所有的用户
	 * @param RoleIds
	 */
	public void deleteBatchByRoleIds(List<Integer> RoleIds) {
		RoleGroupExample example =new RoleGroupExample();
		Criteria criteria = example.createCriteria();
		criteria.andRoleIdIn(RoleIds);
		roleGroupMapper.deleteByExample(example);
	}

	/**
	 * 根据角色id 批量删除所有的用户
	 * @param parseInt
	 */
	public void deleteByRoleId(int roleId) {
		RoleGroupExample example =new RoleGroupExample();
		Criteria criteria = example.createCriteria();
		criteria.andRoleIdEqualTo(roleId);
		roleGroupMapper.deleteByExample(example);
		
	}

	
}
